package com.hxzy.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annotation.Log;
import com.hxzy.common.constant.UserConstants;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.utils.poi.ExcelUtil;
import com.hxzy.system.domain.SysPost;
import com.hxzy.system.domain.dto.SysPostSearchDTO;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.system.service.ISysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 * 
 * @author
 */
@Api(tags = "岗位API")
@RestController
@RequestMapping("/api/system/post")
public class SysPostController extends BaseController
{
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @ApiOperation(value = "分页查询")
    @PreAuthorize("@hxzy.hasPermi('system:post:list')")
    @GetMapping("/list")
    public R list(SysPostSearchDTO sysPostSearchDTO)
    {

        IPage<SysPost> page = postService.selectPostList(sysPostSearchDTO);
        return  super.pageVO(page);
    }

    @ApiOperation(value = "excel导出")
    @PreAuthorize("@hxzy.hasPermi('system:post:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPostSearchDTO sysPostSearchDTO)
    {
        List<SysPost> list = postService.searchExport(sysPostSearchDTO);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list, "岗位数据");
    }


    @ApiOperation(value = "根据岗位编号获取详细信息")
    @PreAuthorize("@hxzy.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public R getInfo(@PathVariable Long postId)
    {
        SysPost sysPost = this.postService.getById(postId);
        sysPost.setUpdateBy(null);
        sysPost.setUpdateBy(null);
        return R.okHasData(sysPost);
    }


    @ApiOperation(value = "新增岗位")
    @PreAuthorize("@hxzy.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysPost post)
    {
        ValueExistsDTO postNameExists=new ValueExistsDTO(post.getPostId(),post.getPostName());
        int count=postService.checkPostNameUnique(postNameExists);
        if(count>0){
            R r=R.build(AckCode.FAIL);
            r.setMsg("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
             return r ;
        }

        ValueExistsDTO postCodeExists=new ValueExistsDTO(post.getPostId(),post.getPostCode());
        int count2=postService.checkPostCodeUnique(postCodeExists);
         if ( count2>0 )
        {
            R r=R.build(AckCode.FAIL);
            r.setMsg("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
            return r ;
        }

        return toAjax(postService.save(post));
    }


    @ApiOperation(value = "修改岗位")
    @PreAuthorize("@hxzy.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysPost post)
    {
        ValueExistsDTO postNameExists=new ValueExistsDTO(post.getPostId(),post.getPostName());
        int count=postService.checkPostNameUnique(postNameExists);
        if(count>0){
            R r=R.build(AckCode.FAIL);
            r.setMsg("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
            return r ;
        }

        ValueExistsDTO postCodeExists=new ValueExistsDTO(post.getPostId(),post.getPostCode());
        int count2=postService.checkPostCodeUnique(postCodeExists);
        if ( count2>0 )
        {
            R r=R.build(AckCode.FAIL);
            r.setMsg("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
            return r ;
        }

        return toAjax(postService.updateById(post));
    }


    @ApiOperation(value = "删除岗位")
    @PreAuthorize("@hxzy.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R remove(@PathVariable Long[] postIds)
    {
        return toAjax(postService.deletePostByIds(postIds));
    }


    @ApiOperation(value = "获取岗位选择框列表")
    @GetMapping("/optionselect")
    public R optionselect()
    {
        List<SysPost> posts = postService.selectPostAll();
        return R.okHasData(posts);
    }
}
